package com.lzw.mall.admin.service.impl;

import com.lzw.mall.admin.entity.Menu;
import com.lzw.mall.admin.mapper.MenuMapper;
import com.lzw.mall.admin.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzw.mall.admin.vo.HomeInfoVo;
import com.lzw.mall.admin.vo.InitVO;
import com.lzw.mall.common.exception.BusinessException;
import com.lzw.mall.common.vo.LayuiTableVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lizhenwei
 * @since 2023-03-17
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
    @Autowired
    private MenuMapper MenuMapper;

    @Override
    public LayuiTableVO doList() {
        LayuiTableVO vo=new LayuiTableVO();
        vo.setData(MenuMapper.selectList01());
        //System.out.println(vo.getData());
        return vo;
    }

    @Override
    public void doDelete(Integer id) {
        //id参数是要删除的菜单的ID
        // 查此菜单是否有子菜单
        // 没有->删除
        // 有->不删除，错误提示
        List<Menu> list=baseMapper.selectByParentId(id);
        if(list.size()>0){
            throw new BusinessException("存在子菜单，不可以删除");
        }
        baseMapper.deleteById(id);

    }

    @Override
    public InitVO init() {
        InitVO vo = new InitVO();
        //通过创建V0对象方式返回json对象
        HomeInfoVo homeInfoVO=new HomeInfoVo();
        homeInfoVO.setTitle("首页");
        homeInfoVO.setHref("");
        vo.setHomeInfo(homeInfoVO);
        //通过创建Map对象方式返回json对象
        Map<String,String> logoMap = new HashMap<>();
        logoMap.put("title","驿站管理系统");
        logoMap.put("image","images/logo.png");
        logoMap.put("href","");
        vo.setLogoInfo(logoMap);
        vo.setMenuInfo(MenuMapper.menuInfo());
        return vo;
    }
}
